<?php
namespace Mp\Service\Grid;
use Closure;
use Illuminate\Contracts\Support\Renderable;
use Mp\Service\Grid\Column;
use Illuminate\Support\Fluent;
use Mp\Service\Grid\Pager;
use Illuminate\Database\Eloquent\Collection;
use Illuminate\Database\Eloquent\Model;
use Illuminate\Support\Facades\Input;
use Illuminate\Database\Eloquent\Builder;


class Grid extends Fluent implements Renderable {

	public  $builder;

	protected $columns = [];	//字段  ['id'=>['name'=>'id','label'=>'序号']]

	protected $source = [] ; //数据源

	protected $pager;	//分页

	protected $data = []; 

	//protected $display = '';


//	protected $header = [];  //表格头信息

	protected $view = "Mp::grid.grid";

	public function __construct(){

	//	$this->_init();
//		$this->pager = new Pager();
	}

	public function builder($builder){

		if($builder instanceof Builder){
			$this->builder = $builder;
		}elseif($builder instanceof Closure){
			call_user_func_array([$this , 'builder'], [call_user_func($builder,$this)]) ;
		}

		return $this;
	}

	private function _init(){
		$this->data = [
			'columns' => $this->columns,
			'source' => $this->source ?: $this->builder->get(),
			'pager' => $this->pager,
		];

		return $this;
	}

	
	//直接传入集合
	public function grid($content){

		//设置数据源
		$this->source($content);
		//设置字段
		if($this->source instanceof Collection){
			$first = $this->source->toArray();	
			$first = reset($first);
			$this->_columnHeader(array_keys($first));
		}

		//设置分页
		/*$this->pager();*/
		return $this;
	}


	//添加字段
	public function addColumn(Column $column){	

		if($column->hidden){
			unset($this->columns[$column->name]);
		}else{
			$this->columns = array_merge($this->columns , [$column->name => $column]);
		}
		
		return $this;
	}

	public function source($content){
		if($content instanceof Closure){
			call_user_func( $content , $this);
			
		}elseif($content instanceof Collection){
			$this->source = $content;

		}
		$this->data['source'] = $this->source;
		return $this;		
	}

	private function _columnHeader($columns = [] ){
		
		foreach($columns as $column){
			if(is_string($column)){
				$this->addColumn(new Column(['name'=>$column]));
			}
		}
		
	//	$this->data['columns'] =  $this->columns;

		return $this;
	}

	public function columns(...$columns ){

		foreach($columns as $column){
			if($column instanceof Column){
				$this->addColumn($column);
			}elseif(is_array($column)){
				$this->addColumn(new Column($column));
			}
		}
		
	//	$this->data['columns'] =  $this->columns;

		return $this;
	}

	public function columnDisplay(String $column_str,Closure $callback){
		$column = collect($this->columns)->firstWhere('name',$column_str);

		$column->display($callback);
		return $this;
	}

	public function hidden(...$names){
		$this->columns = array_except($this->columns,(array)$names);
	//	$this->data['columns'] =  $this->columns;
		return $this;
	}

	/*public function display(Closure $callback){
		$this->display = $callback;
		return $this;
	}*/

	public function pager($pagesize = 15){

		$this->pager = new Pager;
		$this->pager->builder($this->builder)->paginate($pagesize);
		$this->pager = $this->pager->data();
		return $this;
	}



	public function render(){
		
		$this->_init();

		$this->data = array_merge($this->data,$this->attributes);	

		return view($this->view,$this->data)->render();
	}

	public function __toString(){

		return $this->render();
	}



}